
  <template>
  <!-- 招聘规划审批-->
  <div class="bf-commont-template">
    <div class="clearfix bf-top-button">
      <div class="float-r">
        <div class="float-l">
          <el-input placeholder="请输入申请名称搜索" prefix-icon="el-icon-search" v-model="searchData" @keyup.enter.native="quickSearch"></el-input>
        </div>
        <div class="float-l padding-l">
          <el-button @click="showSearch" type="primary">高级筛选</el-button>
        </div>
      </div>
    </div>
    <el-tabs v-model="activeName" @tab-click="handleClick" class="margin-l margin-r">
      <el-tab-pane :label="'待审批'+ tabs.waitVett" name="waitVett">
        <bf-table :data="userData" :setting="setting" :rowAction="waitRowAction" @editSubmit="editSubmit" @pageChange="pageChange" ref="table"></bf-table>
      </el-tab-pane>
      <el-tab-pane :label="'已审批'+ tabs.vetted" name="vetted">
        <bf-table :data="userData" :setting="setting" :rowAction="vettedRowAction" @editSubmit="editSubmit" @pageChange="pageChange" ref="table"></bf-table>
      </el-tab-pane>
    </el-tabs>

    <!-- 高级搜索 -->
    <bf-table-search :editForm='searchSetting' :title="'高级搜索'" ref='searchRosterRef' @formSubmit='searchRosterSubmit'></bf-table-search>
    <!-- 审批 -->
    <bf-table-edit :editRules="editRules" :editForm='approveSetting' :title="'规划审批'" :submitButtons="submitButtons" ref='convertFullRosterRef'>
      <template slot="table">
        <div class="bf-table-title clearfix">
          <div>招聘人员详情:</div>
          <div> </div>
        </div>
        <bf-table :data="planData" :setting="planSetting" :hiddenPagination="true" ref="table"></bf-table>
      </template>
    </bf-table-edit>
    <!--详情-->
    <bf-table-detail ref="bfTableDetail" :title="'规划详情'" :editForm="detailSetting" :showHistory='true' :approveSetting='approveSetting1'>
      <template slot="table">
        <div class="bf-table-title clearfix">
          <div>招聘人员详情:</div>
          <div> </div>
        </div>
        <bf-table :data="planData" :setting="planSetting" :hiddenPagination="true" ref="table"></bf-table>
      </template>
    </bf-table-detail>
  </div>
</template>

<script>

import { searchSetting, setting, approveSetting, detailSetting } from './table.setting.js'
import { getPosition, getRequireList, getApprovedList, approvePlanPass, getPlanaudit, approvePlanNo, getPlanDetail } from './api.js'
export default {
  data() {
    return {
      searchSetting: searchSetting,
      editRules: {
        approvalOpinion: [
          { required: true, message: '审批意见必填', trigger: 'blur' }
        ]
      },
      approveSetting1: {
        procDefKey: 'recruit-plan',
        businessKey: ''
      },
      planData: [],
      searchData: '',
      userData: {},
      statusData: {},
      tabs: {
        waitVett: 0,
        vetted: 0
      },
      submitButtons: [
        {
          title: '通过',
          click: (form) => {
            form.action = 1
            this.approvalRosterSubmit(form)
          }
        },
        {
          title: '不通过',
          click: (form) => {
            form.action = 0
            this.approveNoSubmit(form)
          }
        },
        {
          title: '取消',
          validate: false,
          click: (form) => {
            this.$refs.convertFullRosterRef.hiden()
          }
        }
      ],
      activeName: 'waitVett',
      waitRowAction: [
        {
          label: '查看',
          click: (item) => {
            const params = {
              orgId: item.scope.row.orgId
            }
            const options = []
            getPosition(params).then((response) => { // 根据部门查询岗位
              if (response.data !== undefined) {
                if (response.success) {
                  response.data.forEach((v, index) => {
                    const obj = {
                      value: v.postId,
                      name: v.postName
                    }
                    options.push(obj)
                  })
                  this.planSetting.forEach(v => {
                    if (v.field === 'position') {
                      v.options = options
                    }
                  })
                }
              }
            })
            this.approveSetting1.businessKey = item.scope.row.planId
            this.findDetailById(item.scope.row.planId, this.$refs.bfTableDetail.show)
          }
        },
        {
          label: '审批',
          click: (item) => {
            if (item.scope.row.taskDefKey === 'approve') {
              this.findDetailById(item.scope.row.planId, this.$refs.convertFullRosterRef.show)
            } else {
              this.$message({
                message: '该阶段不可审批',
                type: 'error'
              })
            }
          }
        }
      ],
      vettedRowAction: [
        {
          label: '查看',
          click: (item) => {
            this.approveSetting1.businessKey = item.scope.row.planId
            this.findDetailById(item.scope.row.planId, this.$refs.bfTableDetail.show)
          }
        }
      ], planSetting: [
        {
          field: 'position',
          title: '岗位',
          type: 'selected',
          isParams: true,
          paramsId: 'POST'
        },
        {
          field: 'recruitsNumbers',
          title: '招聘人数',
          type: 'text'
        },
        {
          field: 'remarks',
          title: '备注',
          type: 'text'
        }
      ],
      editPlanSetting: [
        {
          field: 'position',
          title: '岗位',
          type: 'selected',
          isParams: true,
          paramsId: 'POST',
          disableEdit: true
        },
        {
          field: 'recruitsNumbers',
          title: '招聘人数',
          type: 'text'
        },
        {
          field: 'remarks',
          title: '备注',
          type: 'text'
        }
      ],
      setting: setting,
      approveSetting: approveSetting, // 审批页面
      detailSetting: detailSetting, // 详情页面
      searchParams: { // 搜索条件
        page: 1
      }
    }
  },
  mounted: function() {
    this.searchList()
    this.getTabs()
  },
  methods: {
    findDetailById(id, showCall) { // 详情
      getPlanDetail(id).then((response) => {
        // console.log(response.data)
        this.planData = response.data.detailList
        showCall(response.data)
      })
    },

    searchRosterSubmit(form) { // 高级搜索
      this.searchParams.orgId = form.orgId
      this.searchParams.status = form.status
      this.searchParams.planId = form.planId
      this.searchParams.planName = form.planName

      this.searchList()
      this.$refs.searchRosterRef.hiden()
    },
    approvalRosterSubmit(form) { // 审批通过
      approvePlanPass(form).then((response) => {
        if (response.success) {
          this.$message({
            message: '操作成功',
            type: 'success'
          })
          this.$refs.convertFullRosterRef.hiden()
          this.searchList()
          this.getTabs()
        }
      })
    },
    approveNoSubmit(form) { // 审批不通过
      approvePlanNo(form).then((response) => {
        if (response.success) {
          this.$message({
            message: '操作成功',
            type: 'success'
          })
          this.$refs.convertFullRosterRef.hiden()
          this.searchList()
          this.getTabs()
        }
      })
    },


    searchList() {
      const pageParams = this.searchParams
      if (this.activeName === 'waitVett') {
        getRequireList(pageParams).then((response) => {
          this.userData = response
        })
      } else {
        getApprovedList(pageParams).then((response) => {
          this.userData = response
        })
      }
    },
    getTabs() {
      getPlanaudit('').then((request) => {
        this.statusData = request.data
        this.tabs.waitVett = request.data[0].count4Status1
        this.tabs.vetted = request.data[0].count4Status2
      })
    },
    editSubmit(params, formType, formList) {
      // console.log(params, formType, formList)
    },
    pageChange(pageObj) {
      this.searchParams.page = pageObj.page
      this.searchParams.pageSize = pageObj.pageSize
      this.searchList()
    },
    showSearch() {
      this.$refs.searchRosterRef.show()
    },
    showAdd() {
      this.$refs.table.showAdd()
    },
    quickSearch() {
      this.searchParams.page = 1
      this.searchParams.planName = this.searchData
      this.searchList()
    },
    handleClick(tab, event) {
      this.searchParams.page = 1
      this.searchList()
    }
  }
}
</script>

<style scoped>

</style>
